/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using Cysharp.Threading.Tasks;
using System;
using static IQIGame.Onigao.Game.MessageWrapper;

namespace NetProtocol.Client {
	/// <summary>
	/// 中心服排行模块
	/// module : centerRank
	/// </summary>
	public interface CNetCenterRank : INetClientModule {
		
		public ISession GetSession();
	
		Dictionary<int, INetClientModule.Package2MessageWrapper> INetClientModule.GetModuleFunction()
        {
            MessageFunction getRankResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<GetRankResultData>)wrapper;
                SC_getRankResult(session, ref _wrapper.message);
                MessageWrapperPool<GetRankResultData>.Put(_wrapper);
            };
            MessageFunction getRankUserDataResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<GetRankUserDataResultData>)wrapper;
                SC_getRankUserDataResult(session, ref _wrapper.message);
                MessageWrapperPool<GetRankUserDataResultData>.Put(_wrapper);
            };
            MessageFunction getRankGoalieResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<GetRankGoalieResultData>)wrapper;
                SC_getRankGoalieResult(session, ref _wrapper.message);
                MessageWrapperPool<GetRankGoalieResultData>.Put(_wrapper);
            };
        
            Dictionary<int, INetClientModule.Package2MessageWrapper> retDic = new()
            {
                {NetMessageId.centerRank_getRankResult, (session, package) => {
                	var result = MessageWrapperPool<GetRankResultData>.Get();
                	result.Init(session, getRankResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.centerRank_getRankUserDataResult, (session, package) => {
                	var result = MessageWrapperPool<GetRankUserDataResultData>.Get();
                	result.Init(session, getRankUserDataResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.centerRank_getRankGoalieResult, (session, package) => {
                	var result = MessageWrapperPool<GetRankGoalieResultData>.Get();
                	result.Init(session, getRankGoalieResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
            };
            return retDic;
        }
#region Client-side Interface
		/// <summary>
        /// myRanking(-1未参与),rankType(1精确排名,2百分比排名)
        /// </summary>
		public void SC_getRankResult(ISession session, ref GetRankResultData message) 
		{
            _SC_getRankResult(session, ref message);
		}
		
		public void _SC_getRankResult(ISession session, ref GetRankResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.centerRank_getRankResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.centerRank_getRankResult, message.code);
			MsgAsyncAwaiter<GetRankResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_getRankUserDataResult(ISession session, ref GetRankUserDataResultData message) 
		{
            _SC_getRankUserDataResult(session, ref message);
		}
		
		public void _SC_getRankUserDataResult(ISession session, ref GetRankUserDataResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.centerRank_getRankUserDataResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.centerRank_getRankUserDataResult, message.code);
			MsgAsyncAwaiter<GetRankUserDataResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_getRankGoalieResult(ISession session, ref GetRankGoalieResultData message) 
		{
            _SC_getRankGoalieResult(session, ref message);
		}
		
		public void _SC_getRankGoalieResult(ISession session, ref GetRankGoalieResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.centerRank_getRankGoalieResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.centerRank_getRankGoalieResult, message.code);
			MsgAsyncAwaiter<GetRankGoalieResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
#endregion
#region Server-side Interface
		/// <summary>
        /// rankId(1:世界boss)
        /// </summary>
    	public UniTask<GetRankResultData> CS_getRank(int rankId, int count, bool needUserData)
        {
            return _CS_getRank(rankId, count, needUserData);
        }
        
        public UniTask<GetRankResultData> _CS_getRank(int rankId, int count, bool needUserData)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(GetRankResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.centerRank_getRank, "Send->"+"CenterRank.CS_getRank { "
				+ "rankId="
				+ rankId
				+ ", count="
				+ count
				+ ", needUserData="
				+ needUserData
				+" }");
#endif
        	UniTask<GetRankResultData> task = MsgAsyncAwaiter<GetRankResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_getRank(rankId, count, needUserData));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.centerRank_getRankResult);
			return task;
        }
		
		public static SendPackage Pack_for_getRank(int rankId, int count, bool needUserData) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerRank_getRank;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(rankId);
			_out.Write_int(count);
			_out.Write_boolean(needUserData);
			return si_p;
		}
		/// <summary>
        /// rankId(1:世界boss)
        /// </summary>
    	public UniTask<GetRankUserDataResultData> CS_getRankUserData(int rankId, bool lastRanking, int rank)
        {
            return _CS_getRankUserData(rankId, lastRanking, rank);
        }
        
        public UniTask<GetRankUserDataResultData> _CS_getRankUserData(int rankId, bool lastRanking, int rank)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(GetRankUserDataResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.centerRank_getRankUserData, "Send->"+"CenterRank.CS_getRankUserData { "
				+ "rankId="
				+ rankId
				+ ", lastRanking="
				+ lastRanking
				+ ", rank="
				+ rank
				+" }");
#endif
        	UniTask<GetRankUserDataResultData> task = MsgAsyncAwaiter<GetRankUserDataResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_getRankUserData(rankId, lastRanking, rank));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.centerRank_getRankUserDataResult);
			return task;
        }
		
		public static SendPackage Pack_for_getRankUserData(int rankId, bool lastRanking, int rank) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerRank_getRankUserData;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(rankId);
			_out.Write_boolean(lastRanking);
			_out.Write_int(rank);
			return si_p;
		}
		/// <summary>
        /// rankId(1:世界boss)
        /// </summary>
    	public UniTask<GetRankGoalieResultData> CS_getRankGoalie(int rankId, int rankType, int ranking)
        {
            return _CS_getRankGoalie(rankId, rankType, ranking);
        }
        
        public UniTask<GetRankGoalieResultData> _CS_getRankGoalie(int rankId, int rankType, int ranking)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(GetRankGoalieResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.centerRank_getRankGoalie, "Send->"+"CenterRank.CS_getRankGoalie { "
				+ "rankId="
				+ rankId
				+ ", rankType="
				+ rankType
				+ ", ranking="
				+ ranking
				+" }");
#endif
        	UniTask<GetRankGoalieResultData> task = MsgAsyncAwaiter<GetRankGoalieResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_getRankGoalie(rankId, rankType, ranking));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.centerRank_getRankGoalieResult);
			return task;
        }
		
		public static SendPackage Pack_for_getRankGoalie(int rankId, int rankType, int ranking) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerRank_getRankGoalie;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(rankId);
			_out.Write_int(rankType);
			_out.Write_int(ranking);
			return si_p;
		}
#endregion
#region Messages
        public struct GetRankResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public int rankId;
	        public bool isOpen;
	        public List<RankingPOD> rankings;
	        public List<RankingPOD> lastRankings;
	        public int myRanking;
	        public long myScore;
	        public string customData;
	        public int rankType;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                rankId = _in.HasRemaining() ? _in.Read_int() : default;
                isOpen = _in.HasRemaining() ? _in.Read_boolean() : default;
                rankings = _in.HasRemaining() ? _in.Read_list_pod<RankingPOD>(RankingPOD.DynamicFactory) : default;
                lastRankings = _in.HasRemaining() ? _in.Read_list_pod<RankingPOD>(RankingPOD.DynamicFactory) : default;
                myRanking = _in.HasRemaining() ? _in.Read_int() : default;
                myScore = _in.HasRemaining() ? _in.Read_long() : default;
                customData = _in.HasRemaining() ? _in.Read_string() : default;
                rankType = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.centerRank_getRankResult;}
            
            public override string ToString()
            {
                return "CenterRank.getRankResult"+ ", code="+ code+ ", rankId="+ rankId+ ", isOpen="+ isOpen+ ", rankings="+ rankings.ToString<RankingPOD>()+ ", lastRankings="+ lastRankings.ToString<RankingPOD>()+ ", myRanking="+ myRanking+ ", myScore="+ myScore+ ", customData="+ customData+ ", rankType="+ rankType;
            }
        }
        public struct GetRankUserDataResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public string userData;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                userData = _in.HasRemaining() ? _in.Read_string() : default;
            }
            
            public int GetMessageId() {return NetMessageId.centerRank_getRankUserDataResult;}
            
            public override string ToString()
            {
                return "CenterRank.getRankUserDataResult"+ ", code="+ code+ ", userData="+ userData;
            }
        }
        public struct GetRankGoalieResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public RankingPOD rankingPOD;
	        public int ranking;
	        public int rankType;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                rankingPOD = _in.HasRemaining() ? _in.Read_pod<RankingPOD>(RankingPOD.DynamicFactory) : default;
                ranking = _in.HasRemaining() ? _in.Read_int() : default;
                rankType = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.centerRank_getRankGoalieResult;}
            
            public override string ToString()
            {
                return "CenterRank.getRankGoalieResult"+ ", code="+ code+ ", rankingPOD="+ rankingPOD+ ", ranking="+ ranking+ ", rankType="+ rankType;
            }
        }
    }
#endregion
}